package com.lbl.flowabledemo02;

import com.lbl.flowabledemo02.Configuration.FlowableEngineConfigurationExample;
import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.ProcessEngine;
import org.flowable.engine.ProcessEngineConfiguration;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.impl.cfg.StandaloneProcessEngineConfiguration;
import org.flowable.engine.repository.Deployment;
import org.flowable.image.ProcessDiagramGenerator;
import org.junit.jupiter.api.Test;
import javax.imageio.ImageIO;

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;

@Slf4j
public class FlowableTest01 {

    private static final String url = "jdbc:mysql://8.138.20.76:3306/flowable?serverTimezone=UTC";

    /**
     * 部署流程到数据库中
     * 在非spring环境下的应用
     */
    @Test
    void deploy() {
        log.info("deploy...run");
        //流程引擎的配置对象，关联相关的数据源
        ProcessEngineConfiguration cfg = new StandaloneProcessEngineConfiguration()
                .setJdbcUrl(url)
                .setJdbcDriver("com.mysql.cj.jdbc.Driver")
                .setJdbcUsername("root")
                .setJdbcPassword("admin123")
                .setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);

        //获取流程引擎对象
        ProcessEngine processEngine = cfg.buildProcessEngine();
        log.info(String.valueOf(processEngine));
        //部署流程需要获取
        RepositoryService repositoryService = processEngine.getRepositoryService();
        Deployment deploy = repositoryService.createDeployment()
                .addClasspathResource("process/基础/FirstFlow.bpmn20.xml")
                .name("第一个流程图")
                .deploy();
        log.info("deploy.getId():{}", deploy.getId());

    }

    @Test
    void getFlowablePng(){
        log.info("getFlowablePng...run");
        //流程引擎的配置对象，关联相关的数据源
        ProcessEngineConfiguration cfg = new StandaloneProcessEngineConfiguration()
                .setJdbcUrl(url)
                .setJdbcDriver("com.mysql.cj.jdbc.Driver")
                .setJdbcUsername("root")
                .setJdbcPassword("admin123")
                .setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);

        //获取流程引擎对象
        ProcessEngine processEngine = cfg.buildProcessEngine();
        log.info(String.valueOf(processEngine));
        //部署流程需要获取
        RepositoryService repositoryService = processEngine.getRepositoryService();
        Deployment deploy = repositoryService.createDeployment()
                .addClasspathResource("process/基础/FirstFlow.bpmn20.xml")
                .name("第一个流程图")
                .deploy();
        log.info("deploy.getId():{}", deploy.getId());

        // 使用 ProcessDiagramGenerator 生成流程图的字节数组
        ProcessDiagramGenerator processDiagramGenerator = // 同样需要根据实际配置获取
                FlowableEngineConfigurationExample.getProcessDiagramGenerator();
//        if (processDiagramGenerator!= null) {
//            try {
//                byte[] pngBytes = processDiagramGenerator.generateDiagram(deploy.getId(), "png");
//                try (ByteArrayInputStream bis = new ByteArrayInputStream(pngBytes)) {
//                    BufferedImage image = ImageIO.read(bis);
//                    // 将图片保存到本地文件（可选操作）
//                    ImageIO.write(image, "png", new File("output.png"));
//                } catch (IOException e) {
//                    log.error("Error while handling the PNG file", e);
//                }
//            } catch (Exception e) {
//                log.error("Error generating diagram", e);
//            }
//        } else {
//            log.error("ProcessDiagramGenerator is null");
//        }
    }
}
